package com.chushouya.order.service.api.impl;

import com.chushouya.order.dto.admin.servicetime.ServiceTimeDTO;
import com.chushouya.order.service.api.VisitTimeApiService;
import com.general.framework.core.lang.Dates;
import com.general.framework.core.lang.Strings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class VisitTimeApiServiceImpl implements VisitTimeApiService {

    //上门日期
    private static final Integer[] dayMultiArray = {0, 1, 2, 3, 4, 5, 6, 7};
    //上门时间
    private static final Integer[] hourMultiArray = {8, 10,  12, 14,  16,  18};

    @Override
    public List<ServiceTimeDTO> serviceTimeList() {
        List multiArray = new ArrayList<>();
        Date timeNow = Dates.getTimeNow();
        Integer hours = timeNow.getHours();


        List todayMultiArray = new ArrayList<>();
        //根据当前小时匹配hourMultiArray
        for (int i = 0; i < this.hourMultiArray.length; i++) {
            if (hours < this.hourMultiArray[i] && hours < 19) {
                todayMultiArray.add(hourMultiArray[i]);
            }
        }
        //生成数据
        for (int i = 0; i < this.dayMultiArray.length; i++) {
            List<Integer> _hourMultiArray;
            if (i == 0) {
                _hourMultiArray = todayMultiArray;
            } else {
                _hourMultiArray = java.util.Arrays.asList(hourMultiArray);
            }
            if (_hourMultiArray.size() > 0) {
                ServiceTimeDTO serviceTimeDTO = new ServiceTimeDTO();
                Date dateTime = Dates.addDays(timeNow, i);
                serviceTimeDTO.setDay(Dates.format(dateTime, "yyyy-MM-dd"));
                serviceTimeDTO.setDayDetail(Strings.format("{}({})", Dates.format(dateTime, "MM月dd日"), getWeekOfDate(dateTime)));

                List<ServiceTimeDTO.TimeDTO> timeList = new ArrayList<>();
                //插入现在时间
                if (i == 0 && hours>7) {
                    ServiceTimeDTO.TimeDTO timeDTO = serviceTimeDTO.new TimeDTO();
                    timeDTO.setStartTime(hours);
                    timeDTO.setEndTime(hours + 2);
                    timeDTO.setStartMinute(timeNow.getMinutes());
                    timeDTO.setEndMinute(timeNow.getMinutes());
                    Integer minute = timeNow.getMinutes();
                    Integer hour = timeNow.getHours();
                    timeDTO.setTime(Strings.format("{}:{}-{}:{}", hour, minute>10?minute:"0"+minute, hour + 2, minute>10?minute:"0"+minute));
                    timeList.add(timeDTO);
                }
                for (Integer hour : _hourMultiArray) {
                    ServiceTimeDTO.TimeDTO timeDTO = serviceTimeDTO.new TimeDTO();
                    timeDTO.setStartTime(hour);
                    timeDTO.setEndTime(hour + 2);
                    timeDTO.setStartMinute(0);
                    timeDTO.setEndMinute(0);
                    timeDTO.setTime(Strings.format("{}:00-{}:00", hour, hour + 2));
                    timeList.add(timeDTO);
                }
                serviceTimeDTO.setTimeList(timeList);
                multiArray.add(serviceTimeDTO);
            }
        }
        return multiArray;
    }

    /**
     * 获取周
     */
    public static String getWeekOfDate(Date dateTime) {
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateTime);
        Integer week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (week < 0) {
            week = 0;
        }
        return Strings.format("周{}", Dates.WEEK_DAYS_CN[week]);
    }
}
